#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 00:26:13 2018
@project: 天池比赛-A股主板上市公司公告信息抽取
@group: MZH_314
@author: LHQ
"""
import os
import pandas as pd

from reportIE.utils.load_data import load_zengjianchi_train_data, load_train_data


def process(df, key_columns):
    data = []
    dftmp = df.fillna("NAN")
    for i, row in dftmp.iterrows():
        indexs = []
        for col in key_columns:
            indexs.append(str(row[col]))
        row['key'] = "_".join(indexs)
        data.append(row)
    df_new = pd.DataFrame(data)
    return df_new


if __name__ == "__main__":
    
    path_train = os.path.abspath('../data/[new] FDDC_announcements_round1_train_result_20180616/zengjianchi.train')
    path_submit = os.path.abspath('../data/tmp/zengjianchi.txt')
#    path_submit = os.path.abspath('../data/tmp/zengjianchi_table.txt')
    path_submit = os.path.abspath('../data/tmp/a.txt')
    
    # 公告类型的column
    columns = ["公告id", "股东全称", "股东简称", "变动截止日期", "变动价格", "变动数量", "变动后持股数", "变动后持股比例"]

    key_index = [0, 1, 3]   ## columns 中主键的索引位置
     
    
    #================================
    #================================
        
    key_columns = [columns[i] for i in key_index]
    
    df_train = load_train_data(path_train, columns)
#    df_train = load_zengjianchi_train_data(path_train)
    
    """这边要注意读取方法, read_table还是read_csv"""
#    df_submit = pd.read_csv(path_submit)
    df_submit = pd.read_table(path_submit)

    df_train_new = process(df_train, key_columns)
    df_submit_new = process(df_submit,key_columns)
    
    df_cmp = pd.merge(df_train_new, df_submit_new, left_on="key", right_on="key")
    
    # 标准数据集中该字段不为空的记录数
    pos = 0
    for col in columns:
        pos += df_train[col].dropna().size
    
    # 选手提交结果中该字段不为空的记录数
    act = 0    
    for col in columns:
        act += df_submit[col].dropna().size
    
    # 主键匹配 且 提交字段值=正确字段值 且均不为空
    cor = 0
    for i , row in df_cmp.iterrows():
        for col in columns:
            x = "%s_x" % col
            y = "%s_y" % col
            if row[x] == row[y]:
                cor += 1

    recall = cor / pos
    precision  = cor /act
    
    # F1
    f1 = 2 * recall * precision /(recall + precision)
    print("F1:", f1)    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    